package kr.co.doriclassic.chapter25.chat;

public class QuizProtocol {
	//상태
	private static final int WAITING = 0; 
	private static final int PROBLEM = 1; 
	private static final int ANSWER = 2; 
	//총 문제수
	private static final int NUMPROBLEMS = 3; 
	//현재상태
	private int state=WAITING;
	//현재문제
	private int currentProblem=0;
	//문제
	private String[] problems = {
		"네트워크 처리 패키지는?",	
		"자바의 최신버전은?",	
		"인터넷에서 컴퓨터를 식별하는 주소는?"	
	};
	//답
	private String[] answers = {
			"java.io",	
			"1.7",	
			"ip"	
	};
	
	//처리
	public String process(String theInput){
		String theOutput=null;
		//상태에 따라 처리
		switch (state) {
			case WAITING://대기
				theOutput = "퀴즈를 시작합니다(y/n)";
				state=QuizProtocol.PROBLEM;
				break;
			case PROBLEM://문제출제
				if(theInput.equalsIgnoreCase("y")){//대소문자 구분 안함
					theOutput=problems[currentProblem];
					state=QuizProtocol.ANSWER;
				}else{
					state = QuizProtocol.WAITING;
					theOutput="quit";
				}
				break;
			case ANSWER://답변받기
				if(theInput.equalsIgnoreCase(answers[currentProblem])){
					theOutput = "정답. 계속?(y/n)";
					state=QuizProtocol.PROBLEM;
				}
				else{
					state=QuizProtocol.PROBLEM;
					theOutput = "오답. 계속?(y/n)";
				}
				currentProblem = (currentProblem+1)%NUMPROBLEMS;
				break;
		}
		return theOutput;
	}
	
	
}
